package com.cyq.day07;

import java.util.Scanner;

/*

 */
public class ATM {
    static String[][] sysUsers = {
            {"aaaaaa", "111111", "1000"},
            {"bbbbbb", "222222", "2000"},
            {"cccccc", "333333", "3000"},
            {"dddddd", "444444", "4000"},
            {"eeeeee", "555555", "5000"}
    };
    public static void process(){
            int loginReturn = login();
        while (true) {
            Scanner scanner = new Scanner(System.in);
            menu();
            int userInput = scanner.nextInt();
            switch (userInput) {
                case 1:
                    save(loginReturn);
                    break;
                case 2:
                    get(loginReturn);
                    break;
                case 3:
                    search(loginReturn);
                    break;
                case 4:
                    transfer(loginReturn);
                    break;
                case 5:
                    System.exit(0);
                case 6:
                    login();
                    break;
                case 7:
                    changePwd(loginReturn);
                    break;
                default:
                    System.out.println("输入有误");
                    break;
            }
        }
    }
    public static void main(String[] args) {
        process();
    }
    public static int login(){
        int count = 0;
        outer:for (int i = 1; i <= 3; i++) {
            System.out.println("请输入账号:");// 提示用户输入账户
            Scanner userSc = new Scanner(System.in);
            String userIn = userSc.next();
            System.out.println("请输入密码:");// 提示用户输入密码
            Scanner passwordSc = new Scanner(System.in);
            String passwordIn = passwordSc.next();
            for (int j = 0; j <  sysUsers.length; j++) {// 判断输入账户和密码是否匹配数据库
                if ( sysUsers[j][0].equals(userIn) && sysUsers[j][1].equals(passwordIn)) {
                    count = j;// 匹配成功，将当前账户在数据库中的账户组的下标值赋值给count
                    break outer;
                } else if (i == 1 && j == sysUsers.length - 1) {// 第一次输入错误
                    System.out.println("账号或密码错误，请重新输入！");
                } else if (i == 2 && j == sysUsers.length - 1) {// 第二次输入错误
                    System.out.println("账号或密码错误，请重新输入！");
                    System.out.println("这是您的最后一次输入机会，若账号或密码错误，将锁定您的银行卡");
                } else if (j == sysUsers.length - 1) {// 第三次输入错误
                    System.out.println("您的错误次数超过限制，请联系相关银行！");
                    System.exit(0);// 三次错误后，程序终止
                }
            }
        }

        return count;
    }
    public static void menu(){
        System.out.println("请选择：1-存款 2-取款 3-查询 4-转账 5-退出 6-切换用户 7-修改密码");
    }
    public static void save(int count){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入想要存款的金额");
        int deposit = scanner.nextInt();
        sysUsers[count][2] = Double.parseDouble(sysUsers[count][2]) + deposit + "";
        System.out.println("账户的余额为" +sysUsers[count][2]);
    }
    public static void get(int count){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入想要取出的金额");
        int withDraw = scanner.nextInt();
        if (Double.parseDouble(sysUsers[count][2])>=withDraw) {
            sysUsers[count][2] = Double.parseDouble(sysUsers[count][2]) - withDraw + "";
            System.out.println("账户的余额为" + sysUsers[count][2]);
        }else {
            System.out.println("余额不足");
        }
    }
    public static void search(int count){
        System.out.println("查询账户的余额为" +  sysUsers[count][2]);
    }
    public static void transfer(int count){
        Scanner scanner = new Scanner(System.in);
        boolean isExist=false;
        System.out.println("请输入想要转账的账户的用户名");
        String userId = scanner.next();
        if (userId.equals(sysUsers[count][0])){
            System.out.println("不能转给自己");
            return;
        }
        int tranSportID = 0;
        for (int i = 0; i <  sysUsers.length; i++) {
            if (userId.equals(sysUsers[i][0])) {
                tranSportID = i;
                isExist=true;
            }
        }
        if (!isExist){
            System.out.println("你输入的账号不存在请重新输入");
            return;
        }
        System.out.println("请输入想要转入的金额");
        double tranSport = scanner.nextDouble();
        if (tranSport>=Double.parseDouble(sysUsers[count][2])){
            System.out.println("余额不足不能转账");
            return;
        }
        sysUsers[count][2] = Double.parseDouble( sysUsers[count][2]) - tranSport + "";
        sysUsers[tranSportID][2] = Double.parseDouble( sysUsers[tranSportID][2]) + tranSport + "";
        System.out.println("账户的余额为" +  sysUsers[count][2]);
        System.out.println("转入账户的余额为" + sysUsers[tranSportID][2]);
    }
    public static void changePwd(int count){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入想要修改的密码");
        String newPwd= scanner.next();
        sysUsers[count][1]=newPwd;

    }
}
